{extend name="public/baseLayuimini"}
{block name="title"}
<title> 123</title>
{/block}
{block name="css"}

<style>

</style>
{/block}
{block name="main"}
<div class="header" style="display: flex;justify-content: space-between;">
    <div class="left"></div>
    <button class="layui-btn ayui-btn-normal">新增</button>
</div>
<div class="main">
    <table id="table" lay-filter="table"></table>
</div>
<script type="text/html" id="tool">
    <a href="javascript:;" class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
</script>
{/block}
{block name="js"}
<script>
    layui.use(['table','layer','jquery'],function(){
        var table = layui.table,layer = layui.layer,$ = layui.jquery
        table.render({
            elem:'#table',
            url:'/admin/company',
            type:'get',
            page:false,
            cols:[[ //表头
                {field: 'id', title: 'ID', width:45, fixed: 'left'},
                {field: 'company_name', title: '公司名称'},
                {title: '操作', width:160,templet:"#tool"}
            ]]
        })
        table.on("tool(table)",function(obj){
            var data = obj.data;
            var event = obj.event;
            if(event == "edit"){
                layer.prompt({
                    formType:0,
                    value:data.company_name,
                    title:'公司名称'
                    }, function(val,index,elem){
                    if(val == data.company_name) {
                        layer.msg('没有变化')
                        layer.close(index)
                    }
                    let i2 = layer.load()
                    $.ajax({
                        url:"/admin/editCompany",
                        data:{id:data.id,company_name:val},
                        type:'post',
                        success:res=>{
                            if(res){
                                table.reload('table')
                                layer.closeAll()
                            }else{
                                layer.closeAll()
                                layer.msg('修改失败')
                            }
                        }
                    })
                })
            }else{
                layer.confirm('真的要删掉吗?',function(){
                    $.get('/admin/delCompany',{id:data.id},res=>{
                        if(res){
                            table.reload('table')
                            layer.msg('删除成功')
                        }else{
                            layer.msg('删除失败')
                        }
                    })
                },function(){})
            }
        })
        $(".ayui-btn-normal").click(function(){
            layer.prompt({
                formType:0,
                title:'公司名称'
            }, function(val,index,elem){
                let i2 = layer.load()
                $.ajax({
                    url:"/admin/editCompany",
                    data:{company_name:val},
                    type:'post',
                    success:res=>{
                        if(res){
                            table.reload('table')
                            layer.closeAll()
                        }else{
                            layer.closeAll()
                            layer.msg('修改失败')
                        }
                    }
                })
            })
        })
    })
</script>
{/block}